
using System;
using System.Collections.Generic;
using System.Text;

using System.Net;
using System.Data;

using BL.UAClientRemotingInterface;
using BL.BCClientRemotingInterface;
using BL.BAClientRemotingInterface;
using BL.BPClientRemotingInterface;
using BL.BRClientRemotingInterface;
using BL.BBClientRemotingInterface;

using Common.IO.FileIO;

namespace BL.Client.BudgetManageBL
{
    public enum OperateResult
    {
        Success = 0,
        Fail = 1,
        DisConnect = 2,
        WrongInput = 3
    }
    


    public class BudgetManageServerice
    {
        public string UserId;
        private string strServerIp;
        private string strServerPort;
        private int iRetryTimes;

        public IUserAuthority IUA;
        public IBudgetCheck IBC;
        public IBudgetApprove IBA;
        public IBudgetPay IBP;
        public IBudgetReport IBR;
        public IBudgetBorrow IBB;

        #region BudgetManageServerice
        public BudgetManageServerice(string inifilepath)
        {
            IniFileIO file = new IniFileIO(inifilepath);
            this.strServerPort = file.IniReadValue("server", "ServerPort");
            string a = file.IniReadValue("server", "RetryTimes");
            this.iRetryTimes = Convert.ToInt32(file.IniReadValue("server", "RetryTimes"));
            this.strServerIp = file.IniReadValue("server", "ServerIp");
        }

        public BudgetManageServerice(string inifilepath, string server_ip)
        {
            IniFileIO file = new IniFileIO(inifilepath);
            this.strServerPort = file.IniReadValue("server", "ServerPort");
            this.iRetryTimes = Convert.ToInt32(file.IniReadValue("server", "RetryTimes"));
            this.strServerIp = server_ip;
        }

        public BudgetManageServerice(string inifilepath, string server_ip, string server_port)
        {
            IniFileIO file = new IniFileIO(inifilepath);
            this.strServerPort = file.IniReadValue("server", "ServerPort");
            this.iRetryTimes = Convert.ToInt32(file.IniReadValue("server", "RetryTimes"));
            this.strServerIp = server_ip;
            this.strServerPort = server_port;
        }
        #endregion

        #region GetInfo

        public string GetUserId()
        {
            return this.UserId;
        }

        public string GetUserName()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    string user_name = this.IUA.GetUserName(this.UserId);
                    return user_name;
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public string GetUserRealName(string user_id)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    string user_realname = this.IUA.GetUserRealName(user_id);
                    return user_realname;
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public string GetUserNameByUserId(string user_id)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    string user_name = this.IUA.GetUserName(user_id);
                    return user_name;
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public string GetDepartmentName()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    string department_name = this.IUA.GetDepartmentName(this.UserId);
                    return department_name;
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public string GetDeptNameByUserId(string user_id)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    string department_name = this.IUA.GetDepartmentName(user_id);
                    return department_name;
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public string GetDeptPNameByUserId(string user_id)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    string department_name = this.IUA.GetDepartmentPName(user_id);
                    return department_name;
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public DataSet GetDeptBudgetType(string department_id)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return IBC.GetDeptBudgetType(department_id);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public string GetUserIdBySN(string department_sn, string user_password)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IUA.GetUserIdBySN(department_sn, user_password);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        #endregion

        #region UserIndenrify
        public OperateResult UserIndentify(string server_ip, string user_name, string user_password)
        {
            if (server_ip == "" || user_name == "" || user_password == "")
            {
                return OperateResult.WrongInput;
            }

            this.IUA = (IUserAuthority)Activator.GetObject(typeof(IUserAuthority),
                    "tcp://" + server_ip + ":" +
                    this.strServerPort + "/RemotingCommon");

            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    if (this.IUA.FUserIndentify(user_name, user_password))
                    {
                        this.IBC = (IBudgetCheck)Activator.GetObject(typeof(IBudgetCheck),
                            "tcp://" + server_ip + ":" +
                                this.strServerPort + "/RemotingCommon");
                        this.IBA = (IBudgetApprove)Activator.GetObject(typeof(IBudgetApprove),
                            "tcp://" + server_ip + ":" +
                                this.strServerPort + "/RemotingCommon");
                        this.IBP = (IBudgetPay)Activator.GetObject(typeof(IBudgetPay),
                            "tcp://" + server_ip + ":" +
                                this.strServerPort + "/RemotingCommon");
                        this.IBR = (IBudgetReport)Activator.GetObject(typeof(IBudgetReport),
                            "tcp://" + server_ip + ":" +
                                this.strServerPort + "/RemotingCommon");
                        this.IBB = (IBudgetBorrow)Activator.GetObject(typeof(IBudgetBorrow),
                            "tcp://" + server_ip + ":" +
                                this.strServerPort + "/RemotingCommon");
                        this.UserId = this.IUA.GetUserId(user_name);
                        this.strServerIp = server_ip;

                        string user_hostname = Dns.GetHostName();
                        IPAddress[] iplist = Dns.GetHostAddresses(user_hostname);
                        string user_ip = iplist[iplist.Length - 1].ToString();
                        this.IUA.RaiseUserLogin(user_name, user_ip, user_hostname);

                        return OperateResult.Success;
                    }
                    else
                        return OperateResult.Fail;
                }
                catch (System.Net.Sockets.SocketException ex)
                {
                    i++;
                    continue;
                }
            }
            return OperateResult.DisConnect;
        }

        

        public bool UserIndentifyBySN(string department_sn, string user_password)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IUA.UserIndentifyBySN(department_sn, user_password);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return false;
        }
        public void UserLogOut()
        {
            try
            {
                string user_hostname = Dns.GetHostName();
                this.IUA.RaiseUserLogout(user_hostname);
            }
            catch (Exception ex)
            {
            }
        }
        #endregion


        #region FinancialBudgetCheck
        public DataSet GetBudgetInfoByState(string budget_state_id)
        {

            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC.GetBudgetInfoByState(budget_state_id);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public DataSet GetBudgetInfoByStateDepartment(string budget_state_id, string department_id)
        {

            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC.GetBudgetInfoByStateDepartment(budget_state_id, department_id);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public bool FBudgetApprove(string budget_id, bool isapproved, string budget_approve_needcount)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBA.FBudgetApprove(budget_id, 
                        this.UserId, isapproved, budget_approve_needcount);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return false;
        }

        public bool HaveFBAAuthority(string budget_id)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBA.HaveFBAAuthority(budget_id, this.UserId);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return false;
        }
       

        public DataSet GetBudgetItemInfo(string budget_id)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC.GetBudgetItemInfo(budget_id);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
 
        }

        
        #endregion

        #region BudgetQuery


        public DataSet GetAllDeptPName()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC.GetAllDeptPName();
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }
        public DataSet GetDeptNameByPId(string department_pid)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC. GetDeptNameByPId(department_pid);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public DataSet GetAllBudgetState()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC.GetAllBudgetState();
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        } 
        public DataSet GetBudgetType()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC.GetBudgetType();
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public DataSet GetBudgetSubType(string budget_type_id)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC.GetBudgetSubType(budget_type_id);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public DataSet GetPurchaseMethod()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC.GetPurchaseMethod();
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public DataSet GetBudgetRange()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC.GetBudgetRange();
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }
        
        public DataSet GetPayMethod()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC.GetPayMethod();
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public DataSet GetBudgetInfo(string budget_range_id, string budget_type_id, string department_id,
            string pay_method_id, string purchase_method_id, string budget_state_id,
            bool isfullinfo)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC.GetBudgetInfo(budget_range_id, budget_type_id, 
                        department_id, pay_method_id,purchase_method_id, budget_state_id, isfullinfo);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public DataSet GetBudgetInfo(string budget_range_id, string budget_type_id, string department_id,
            string pay_method_id, string purchase_method_id, string budget_state_id,
            bool allapproved, bool isfullinfo)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBC.GetBudgetInfo(budget_range_id,budget_type_id,
                        department_id, pay_method_id, purchase_method_id, budget_state_id, 
                        allapproved, isfullinfo);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }


        public DataSet GetBudgetInfoByAuth()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBA.GetBudgetToFApprove(this.UserId);
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        public DataSet GetPayPriceLog()
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return this.IBP.GetPayPriceLog();
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return null;
        }

        #endregion

        #region BudgetReport
        public OperateResult BudgetReport(System.Data.DataTable budgetdata, string user_name)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    if (this.IBR.BudgetReport(budgetdata, user_name))
                        return OperateResult.Success;
                    else
                        return OperateResult.Fail;
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return OperateResult.DisConnect;
        }

        public OperateResult BudgetReport(string budget_range_id, string budget_price,
            string pay_method_id, string purchase_method_id,
            string budget_type_id, string budget_remark, string user_id, string[,] budget_item_info)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    if (this.IBR.BudgetReport(budget_range_id,null, budget_price,
             pay_method_id, purchase_method_id,
             budget_type_id, budget_remark, user_id, budget_item_info))
                        return OperateResult.Success;
                    else
                        return OperateResult.Fail;
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return OperateResult.DisConnect;
        }

        

        #endregion

        #region BudgetPay

        public bool IsLegal(string pay_price, string budget_type_name)
        {
            int i = 0;
            while (i < this.iRetryTimes)
            {
                try
                {
                    return (this.IBP.IsLegal(pay_price, budget_type_name));
                }
                catch (Exception ex)
                {
                    i++;
                    continue;
                }
            }
            return false;

        }
        
        

        #endregion








    }
}
